//
//  find.swift
//  WuZhen
//
//  Created by zouchiming on 2025/5/18.
//

import SwiftUI

struct FindView: View {
    var body: some View {
        NavigationView {
            ScrollView {
                VStack(spacing: 20) {
                    FindPostView(
                        mainPhotoName: "640",
                        subPhoto1Name: "641",
                        subPhoto2Name: "642",
                        locationName: "浙江省乌镇大剧院",
                        textTitle: "十年如约，狂欢入戏！",
                        textContent: "2023年10月19日，万众瞩目的第十届乌镇戏剧节盛大启幕！第十届乌镇戏剧节以"起"为主题——跃起、奋起、迎风起飞，迈向新的旅程！",
                        finderName: "@乌镇戏剧节",
                        finderImage: "hand.draw",
                        finderTime: "10月19日 21:24"
                    )
                    
                    FindPostView(
                        mainPhotoName: "muxin",
                        subPhoto1Name: "mapIamge5",
                        subPhoto2Name: "643",
                        locationName: "浙江省乌镇木心美术馆",
                        textTitle: "木心美术馆",
                        textContent: "木心美术馆致力于纪念和展示画家、文学家、诗人木心先生（1927-2011）的毕生心血与美学遗产。这不仅是一座收藏过去时的美术馆，而且是向未来开放的精神指向和学术空间。美术馆坐落于木心的故乡乌镇，为木心的研究提供了完整的文献。",
                        finderName: "@乌镇风景区",
                        finderImage: "house.fill",
                        finderTime: "3月27日 13:06"
                    )
               
                    FindPostView(
                        mainPhotoName: "644",
                        subPhoto1Name: "645",
                        subPhoto2Name: "646",
                        locationName: "浙江省乌镇西栅风景区",
                        textTitle: "一位素颜绝世美女",
                        textContent: "乌镇，美在原始、质朴，美在不哗众取宠、不矫揉造作。乌镇的美，一如既往，正如《乌镇酒舍歌》所言。东风吹雨如吹尘，野烟漠漠遮游人。须臾云破日光吐，绿波蹙作黄金鳞。",
                        finderName: "@今安在",
                        finderImage: "xbox.logo",
                        finderTime: "7月24日 19:52"
                    )
                }
                
                ProgressView()
            }
            .navigationTitle("发现")
            .navigationBarItems(trailing: HStack(spacing: 10) {
                ButtonUp1()
                ButtonUp2()
            })
            .background(
                ZStack {
                    LinearGradient(colors: [.red, .blue], 
                                 startPoint: .topLeading, 
                                 endPoint: .bottomTrailing)
                    .ignoresSafeArea()
                    
                    Color.ultraThinMaterial
                }
            )
        }
    }
}

struct FindPostView: View {
    let mainPhotoName: String
    let subPhoto1Name: String
    let subPhoto2Name: String
    let locationName: String
    let textTitle: String
    let textContent: String
    let finderName: String
    let finderImage: String
    let finderTime: String
    
    @State private var isShowingDetail = false
    
    var body: some View {
        VStack(spacing: 8) {
            photoGallerySection
            contentSection
            DividerView()
            footerSection
        }
        .background(Color.ultraThinMaterial)
        .cornerRadius(20)
        .padding(.horizontal)
        .sheet(isPresented: $isShowingDetail) {
            PostDetailView(
                images: [mainPhotoName, subPhoto1Name, subPhoto2Name],
                title: textTitle,
                content: textContent,
                finderName: finderName,
                finderImage: finderImage,
                finderTime: finderTime
            )
        }
    }
    
    private var photoGallerySection: some View {
        HStack(spacing: 4) {
            Image(mainPhotoName)
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(width: 175, height: 175)
                .cornerRadius(15)
                .clipped()
            
            VStack(spacing: 4) {
                locationView
                subPhotosView
            }
        }
    }
    
    private var locationView: some View {
        Image("IMG_9026")
            .resizable()
            .aspectRatio(contentMode: .fill)
            .frame(width: 175, height: 86)
            .cornerRadius(15)
            .overlay(
                VStack(spacing: 4) {
                    Image(systemName: "mappin.and.ellipse")
                        .foregroundColor(.white)
                        .background(
                            Circle()
                                .fill(LinearGradient(
                                    colors: [.green, .blue],
                                    startPoint: .leading,
                                    endPoint: .trailing
                                ))
                                .frame(width: 25, height: 25)
                        )
                    
                    Text(locationName)
                        .font(.caption)
                        .foregroundColor(.secondary)
                }
            )
    }
    
    private var subPhotosView: some View {
        HStack(spacing: 4) {
            Image(subPhoto1Name)
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(width: 85, height: 85)
                .cornerRadius(15)
                .clipped()
            
            Image(subPhoto2Name)
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(width: 85, height: 85)
                .cornerRadius(15)
                .clipped()
        }
    }
    
    private var contentSection: some View {
        VStack(spacing: 4) {
            HStack {
                Text(textTitle)
                    .font(.headline)
                    .padding(.horizontal, 8)
                Spacer()
            }
            
            Text(textContent)
                .font(.caption)
                .padding(.horizontal, 8)
        }
        .frame(height: 55)
        .onTapGesture {
            isShowingDetail = true
        }
    }
    
    private var footerSection: some View {
        HStack(spacing: 8) {
            Image(systemName: finderImage)
                .font(.headline)
                .foregroundColor(.white)
                .background(
                    LinearGradient(
                        colors: [.red, .blue],
                        startPoint: .leading,
                        endPoint: .trailing
                    )
                )
                .cornerRadius(40)
            
            Text(finderName)
                .font(.headline)
                .foregroundColor(.blue)
            
            Text(finderTime)
                .font(.caption)
                .foregroundColor(.secondary)
            
            Spacer()
            
            InteractionButtons()
        }
        .padding(.horizontal, 8)
    }
}

struct PostDetailView: View {
    let images: [String]
    let title: String
    let content: String
    let finderName: String
    let finderImage: String
    let finderTime: String
    
    var body: some View {
        ZStack {
            VStack {
                TabView {
                    ForEach(images, id: \.self) { imageName in
                        Image(imageName)
                            .resizable()
                            .aspectRatio(contentMode: .fill)
                            .clipped()
                    }
                }
                .tabViewStyle(PageTabViewStyle())
                .frame(height: 400)
                .frame(maxWidth: .infinity)
                .cornerRadius(30)
             
                HStack {
                    Text(title)
                        .font(.largeTitle)
                        .padding()
                    Spacer()
                }
                
                Text(content)
                    .padding(.horizontal)
                
                DividerView()
                
                HStack(spacing: 8) {
                    Image(systemName: finderImage)
                        .font(.title)
                        .foregroundColor(.white)
                        .background(
                            LinearGradient(
                                colors: [.red, .blue],
                                startPoint: .leading,
                                endPoint: .trailing
                            )
                        )
                        .cornerRadius(40)
                    
                    Text(finderName)
                        .font(.headline)
                        .foregroundColor(.blue)
                    
                    Spacer()
                    
                    Text(finderTime)
                        .font(.headline)
                        .foregroundColor(.secondary)
                        .padding(.horizontal)
                }
                .padding(.horizontal, 8)
                
                Spacer()
                
                InteractionButtons(largeSize: true)
            }
        }
    }
}

struct DividerView: View {
    var body: some View {
        Rectangle()
            .fill(
                LinearGradient(
                    colors: [.red, .blue],
                    startPoint: .leading,
                    endPoint: .trailing
                )
            )
            .frame(height: 2)
    }
}

struct InteractionButtons: View {
    let largeSize: Bool
    @State private var isLike = false
    @State private var isStar = false
    @State private var isShare = false
    
    init(largeSize: Bool = false) {
        self.largeSize = largeSize
    }
    
    var body: some View {
        HStack {
            Spacer()
            
            interactionButton(
                systemName: isLike ? "heart.fill" : "heart",
                label: "喜欢",
                color: .pink,
                isActive: isLike
            ) {
                isLike.toggle()
            }
            
            Spacer()
            
            interactionButton(
                systemName: isStar ? "star.fill" : "star",
                label: "收藏",
                color: .yellow,
                isActive: isStar
            ) {
                isStar.toggle()
            }
            
            Spacer()
            
            interactionButton(
                systemName: isShare ? "square.and.arrow.up.fill" : "square.and.arrow.up",
                label: "分享",
                color: .green,
                isActive: isShare
            ) {
                isShare.toggle()
            }
            .contextMenu {
                ForEach(shareOptions, id: \.name) { option in
                    Button {
                        isShare.toggle()
                    } label: {
                        Label(option.name, systemImage: option.icon)
                    }
                }
            }
            
            Spacer()
        }
    }
    
    private var shareOptions: [(name: String, icon: String)] {
        [
            ("分享微信", "message"),
            ("分享到朋友圈", "circle.hexagongrid.circle"),
            ("分享到抖音", "music.note"),
            ("分享到邮件", "envelope")
        ]
    }
    
    private func interactionButton(
        systemName: String,
        label: String,
        color: Color,
        isActive: Bool,
        action: @escaping () -> Void
    ) -> some View {
        Button(action: action) {
            VStack {
                Image(systemName: systemName)
                    .foregroundColor(color)
                    .scaleEffect(largeSize ? 2 : 1)
                
                if largeSize {
                    Text(label)
                        .font(.headline)
                        .foregroundColor(isActive ? .primary : .secondary)
                        .padding(.top)
                }
            }
        }
    }
}

struct ButtonUp1: View {
    var body: some View {
        Button(action: {}) {
            Image(systemName: "magnifyingglass")
                .foregroundColor(.primary)
        }
    }
}

struct ButtonUp2: View {
    var body: some View {
        Button(action: {}) {
            Image(systemName: "ellipsis")
                .foregroundColor(.primary)
        }
    }
}

struct FindView_Previews: PreviewProvider {
    static var previews: some View {
        FindView()
    }
}